package com.lq.memo.util;

import lombok.Data;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: memo
 * @description: 时间处理类
 * @author: LQ
 * @create: 2021-10-28 16:43
 */
@Data
@Component
public class DateUtil {

    /**
     * 获取年
     *
     * @return
     */
    public Integer getYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取年
     *
     * @param date 获取年份的日期
     * @return
     */
    public Integer getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }


    public Integer getMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1;
    }

    public Integer getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    public Integer getDate() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DATE);
    }

    public Integer getDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    /**
     * 获取距离今天多少天后的日期字符串
     *
     * @param days 多少天后的日期,如果是之前的日期传入负数即可
     * @return
     */
    public String getAffterDateStr(int days) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.DATE, days);
        String daysAfterDate = sdf1.format(calendar.getTime());
        System.out.println(daysAfterDate);
        return daysAfterDate;
    }

    public Date getAfterDate(int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    /**
     * 距离今天多少天前的日期,如果是之后的,传入负数即可
     *
     * @param days
     * @return
     */
    public String getBeforeDateStr(int days) {
        String beforeDate = getAffterDateStr(-days);
        return beforeDate;
    }

    public Date getBeforeDate(int days) {
        Date beforeDate = getAfterDate(-days);
        return beforeDate;
    }

    public String date2MonthDay(Date date) {
        if (date == null) {
            return "";
        }
        Integer month = getMonth(date);
        Integer day = getDate(date);
        return (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
    }

    public static void main(String[] args) throws ParseException {
        /*System.out.println(new DateUtil().getYear(new SimpleDateFormat("yyyy-MM-dd").parse(new DateUtil().getBeforeDate(3) )));
        System.out.println(new DateUtil().getMonth(new SimpleDateFormat("yyyy-MM-dd").parse(new DateUtil().getBeforeDate(3) )));
        System.out.println(new DateUtil().getDate(new SimpleDateFormat("yyyy-MM-dd").parse(new DateUtil().getBeforeDate(3) )));*/
        String s = new DateUtil().date2MonthDay(new Date());
        System.out.println(s);
    }
}
